package cn.ecut.DBConnection;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class OpenConnection {
    public Connection getConnection()
    {
        String driver=null;
        String url = null;
        String user = null;
        String psd = null;
        Connection conn = null;

        Properties pro =new Properties();
        try {
            pro.load(this.getClass().getClassLoader().getResourceAsStream("druid.properties"));
            driver=pro.getProperty("driverClassName");
            url=pro.getProperty("url");
            user = pro.getProperty("username");
            psd=pro.getProperty("password");
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }

        try {
            Class.forName(driver);//指定驱动，表示加载数据库的驱动类
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }

        try {
            conn = DriverManager.getConnection(url,user,psd);//获取连接，格式为“DriverManager.getConnection("URL","用户名","密码")”
            conn.setAutoCommit(false);
            return conn;
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }

        return null;
    }
}
